package com.wy.service.impl.wy;

import com.wy.bean.RepairBean;
import com.wy.constants.CommonConstants;
import com.wy.dao.RepairEntityMapper;
import com.wy.dao.RepairLogMapper;
import com.wy.dao.UserDao;
import com.wy.model.sys.RepairEntity;
import com.wy.model.sys.RepairLog;
import com.wy.model.sys.UserEntity;
import com.wy.service.wy.RepairService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
@Service("repairServiceImpl")
public class RepairServiceImpl implements RepairService {
    @Autowired
    private RepairEntityMapper repairEntityMapper;

    @Autowired
    private RepairLogMapper repairLogMapper;

    @Autowired
    private UserDao userDao;

    @Override
    public List<RepairBean> repairList(int pageSize, int start, String type, String content, String status, String keys, String startTime,
                                       String finishTime, Integer uid, String soon, List<String> rids,Integer ext1) {
        return  repairEntityMapper.repairList(pageSize,start,type,content,status,keys,startTime,finishTime,uid,soon,rids,ext1);
    }

    @Override
    public Integer repairSize(int pageSize, int start, String type,String content, String status, String keys, String startTime,
                              String finishTime,Integer uid,String soon,List<String> rids,Integer ext1) {
        return repairEntityMapper.repairSize(pageSize,start,type,content,status,keys,startTime,finishTime,uid,soon,rids,ext1);
    }

    @Override
    public void insertRepair(RepairEntity repairEntity) {
        repairEntityMapper.insert(repairEntity);
        //添加报修日志
        UserEntity user = userDao.getUserById(repairEntity.getHouseholdId());
        repairLogMapper.insert(generateRepairLog(repairEntity.getId(),user, "0",CommonConstants.PENDING));
    }

    /**
     * 生成维修日志
     * @param repairId  维修单ID
     * @param user  用户ID
     * @param status 维修状态
     * @return
     */
    private RepairLog generateRepairLog(Integer repairId,UserEntity user,String status,String remark) {
        RepairLog repairLog = new RepairLog();
        repairLog.setRepairId(repairId);
        repairLog.setRepairDealTime(new Date());
        repairLog.setRepairDealUser(user.getName());
        repairLog.setRepairStatus(status);
        repairLog.setRepairDealRemark(remark);
        return repairLog;
    }

    @Override
    public void updateRepair(RepairEntity repairEntity) {
        repairEntityMapper.updateByPrimaryKeySelective(repairEntity);
    }

    @Override
    public void delRepair(List<String> repairIds) {
        repairEntityMapper.deleteByPrimaryKeys(repairIds);
    }

    @Override
    public List<RepairLog> getRepairLogList(Integer repairId) {
        return repairLogMapper.getRepairLogList(repairId);
    }

    /**
     * 工作人员分配保修单
     * @param repairId
     * @param uid
     * @return
     */
    @Override
    public boolean assignRepairBill(String loginName,Integer repairId, Integer uid) {
        UserEntity user = userDao.getUserById(uid);
        repairLogMapper.insert(generateRepairLog(repairId,user, "1",String.format(CommonConstants.PROCESSING,user.getName(),user.getMobile())));
        RepairEntity repairEntity = repairEntityMapper.selectByPrimaryKey(repairId);
        repairEntity.setStatus(1);
        repairEntity.setExt1(uid.toString());
        repairEntity.setExt2(user.getName());
        repairEntity.setExt3(user.getMobile());
        repairEntity.setFinishTime(new Date());
        repairEntity.setOperator(loginName);
        repairEntityMapper.updateByPrimaryKeySelective(repairEntity);
        //是否需要建个通知，您的保修单已推送给维修员xxx，我们会及时更新状态
        return true;
    }

    /**
     *  维修员回退保修单，需要物业人员重新分配
     * @param repairId
     * @return
     */
    @Override
    public boolean reassignRepairBill(Integer repairId) {
        RepairEntity repairEntity = repairEntityMapper.selectByPrimaryKey(repairId);
        repairEntity.setStatus(0);
        repairEntityMapper.updateByPrimaryKeySelective(repairEntity);
        return true;
    }

    /**
     * 维修员处理完成保修单
     * @param repairId
     * @param uid
     * @param remark 维修员处理说明
     * @return
     */
    @Override
    public boolean dealRepairBill(Integer repairId,Integer uid,String remark) {
        UserEntity user = userDao.getUserById(uid);
        repairLogMapper.insert(generateRepairLog(repairId,user, "2",CommonConstants.DONE));
        RepairEntity repairEntity = repairEntityMapper.selectByPrimaryKey(repairId);
        repairEntity.setStatus(2);
        repairEntity.setExt4(remark);
        repairEntity.setFinishTime(new Date());
        repairEntity.setOperator(user.getLoginName());
        repairEntityMapper.updateByPrimaryKeySelective(repairEntity);
        return true;
    }

}
